/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gov.cemaden.factory;

import br.gov.cemaden.smp.util.HibernateUtil;
import java.lang.reflect.InvocationTargetException;
import org.hibernate.Session;

/**
 *
 * @author Alan
 */
public class DaoFactory<T> {

    Session session;
    private T o;

    public DaoFactory(Class object) {
        //if(HibernateUtil.getSessionFactory().isClosed())
        session = HibernateUtil.getSessionFactory().openSession();
        //else
        //  session = HibernateUtil.getSessionFactory().getCurrentSession();

        try {
            o = (T) object.getDeclaredConstructor(Session.class).newInstance(session);
        } catch (InstantiationException ex) {
            System.err.printf("InstantiationException ao tentar iniciar a classe %s%n%s", object.getName(), ex);
        } catch (IllegalAccessException ex) {
            System.err.printf("IllegalAccessException ao tentar iniciar a classe %s%n%s", object.getName(), ex);
        } catch (NoSuchMethodException ex) {
            System.err.printf("Erro ao invocar mÃ©todo construtor da classe %s%n%s", object.getName(), ex);
        } catch (SecurityException ex) {
            System.err.printf("Security ao tentar iniciar a classe %s%n%s", object.getName(), ex);
        } catch (IllegalArgumentException ex) {
            System.err.printf("Erro ao passar argumento para a classe %s%n%s", object.getName(), ex);
        } catch (InvocationTargetException ex) {
            System.err.printf("Erro ao tentar invocar construtor ou metodo para a classe %s%n%s", object.getName(), ex);
        }
    }

    public T get() {
        return o;
    }
}
